<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/3/25
 * Time: 20:03
 */

namespace front\modules\web\controllers;

use backfeed\services\CommentService;
use EasyWeChat\Foundation\Application;
use front\controllers\WebBaseController;
use goods\services\CollectionService;
use users\services\UserService;
use wechat\services\AccountService;
use Yii;
use yii\helpers\ArrayHelper;

class MyController extends WebBaseController
{
    public $layout = false;

    public function actionIndex()
    {
        $session = \yii::$app->session;
        yii::info($session->has('openId'));
        if (empty($session->get('openId'))) {
            $conf = AccountService::getWechatConfig(self::ORIGINAL_ID);
            $app = new Application($conf);
            $oauth = $app->oauth;
            Yii::$app->session->set('target_url', 'http://hs.front.10000dt.com/web/my/index');
            $oauth->redirect()->send();
        }
        $data = [];
        $params['user_id'] = $this->getUserId();
        yii::info($params['user_id']);
        if($params['user_id'] == 0)
        {
            return $this->redirect('/web/user/login');
        }
        $data['comm'] = 0;
        $comm = CommentService::getCount($params);
        if($comm['code'] == 0){
            $data['comm'] = $comm['result']['count'];
        }
        //未读消息  暂时默认值 TODO
//        $data['msg'] = 1;
        //当前用户收藏的商品数量
        $data['coll'] = CollectionService::getCollectionCount(null, $params['user_id']);
        $user = UserService::getUserInfo($params['user_id'], 1);
        $user = ArrayHelper::toArray($user['result']);
        $data['name'] = empty($user['nickname']) ? $user['mobile'] : $user['nickname'];
        $data['shop_id'] = $this->shop_id;
        $data['photo'] = $this->photo;
        return $this->render('my',['data'=>$data]);
    }

    public function actionContact()
    {
        return $this->render('contact');
    }

}